<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Email_templates extends Admin_Controller{

    function Email_templates(){
        parent::Admin_Controller();
        $CI = & get_instance();
        $CI->load->module_model('administrator.email_templates.email_template_model');
        $CI->load->module_language('administrator.email_templates.email_template');
        
        $this->base_url = 'admin/email_templates';
        //$this->limit = $this->rows_per_page();
        $this->offset = 3;
        $this->sort_field = 4;
        $this->sort_type = 5;
        $CI->load->module_library('administrator.users.auth');
        $user = $CI->auth->get_auth_data();
        if(!$CI->auth->can_access_settings($user['role_level'], get_class($this))){
            $this->add('no_permisson', 1);
        }
    }
    
    function index(){
        $CI = & get_instance();

        $this->_lists();
        $this->module_toolbar('administrator.email_templates.toolbar');
        $this->view('lists');
        $this->navigation(lang('_title'));
        $this->add('img_user', 'user_off.gif');
        $this->add('img_email', 'email_on.gif');
        $this->add('img_parameters', 'parameters_off.gif');
        $this->add('img_logging', 'logging_off.gif');
		$this->add('img_vehicle', 'vehicle_off.gif');
        $this->add('img_trailer', 'trailer_off.gif');
        $this->add('img_driver', 'driver_off.gif');
        $this->add('img_view_log', 'view_log_off.gif');
		$this->add('img_cost', 'cost_off.gif');
		$this->add('img_dashlet', 'dashboard_off.gif');
    }
    
    function add_new(){
        $CI = & get_instance();

        if ($this->_validate()){
            $data = array(EMAIL_TEMPLATE_SUBJECT => $CI->input->post('subject'),
                          EMAIL_TEMPLATE_KEY => url_title(strtolower($CI->input->post('subject')), '_'),
                          EMAIL_TEMPLATE_BODY => $CI->input->post('body'),
                          );
            if($CI->email_template_model->save($data)){
                set_flash_message(lang('_add_success'));
                redirect($this->base_url);
            }
        }

        $CI->load->library('Fckeditor', '', 'editor');
        $CI->editor->InstanceName = 'body';
        $CI->editor->Height = '300';
        $CI->editor->Value = $CI->input->post('body');
        $this->add('editor', $CI->editor->CreateHtml());
        $this->add('template_id', '');
        $this->module_navigation(lang('_title').' : '. lang('_add_title'));
        $this->view('add_edit');
    }
    
    function _validate(){
        $CI = & get_instance();

        $CI->load->library('form_validation');
        $CI->load->helper('form');
        $CI->form_validation->set_rules('subject', 'Subject', 'trim|required');
        $CI->form_validation->set_rules('body', 'Body', 'trim|required');
        return $CI->form_validation->run();
    }
    
    function edit(){
        $CI = & get_instance();

        $template_id = $CI->uri->segment(3);
        $CI->email_template_model->set_id($template_id);
        if(!$CI->email_template_model->get()){
            set_flash_message(lang('_not_found'));
            redirect($this->base_url);
            return;
        }

        if ($this->_validate()){
            $data = array(EMAIL_TEMPLATE_SUBJECT => $CI->input->post('subject'),
                          EMAIL_TEMPLATE_BODY => $CI->input->post('body'),
                          );
            if($CI->email_template_model->save($data)){
                set_flash_message(lang('_edit_success'));
                redirect($this->base_url);
            }
        }

        $_POST['subject'] = isset($_POST['subject'])?$_POST['subject']:$CI->email_template_model->subject;
        $CI->load->library('Fckeditor', '', 'editor');
        $CI->editor->InstanceName = 'body';
        $CI->editor->Height = '300';
        $CI->editor->Value = isset($_POST['body'])?$CI->input->post('body'):$CI->email_template_model->body;
        $this->add('editor', $CI->editor->CreateHtml());
        $this->add('template_id', $template_id);
        
        $this->module_navigation(lang('_title').' : '. lang('_edit_title'));
        $this->view('add_edit');
    }

    function delete(){
        $CI = & get_instance();

        $CI->email_template_model->set_id($CI->uri->segment(3));
        if(!$CI->email_template_model->get()){
            set_flash_message(lang('_not_found'));
            redirect($this->base_url);
            return;
        }
        $CI->email_template_model->delete();
        set_flash_message(lang('_del_success'));
        redirect($this->base_url);
    }

    function _lists($condition=NULL){
        $CI = & get_instance();
        $base_url = $this->base_url;
        //$limit = $this->limit;
        $offset = $CI->uri->segment($this->offset)?$CI->uri->segment($this->offset):0;
        $sort_field = $CI->uri->segment($this->sort_field)?$CI->uri->segment($this->sort_field):'subject';
        $sort_type = $CI->uri->segment($this->sort_type)?'1':'0';

        $email_templates = $CI->email_template_model->get_lists($condition, NULL, $offset, $sort_field, $sort_type);
        $this->add('templates', $email_templates);

        $CI->load->library('pagination');
        $config['base_url'] = site_url($base_url.'/%s/'.$sort_field.'/'.$sort_type);
        $config['total_rows'] = $CI->email_template_model->get_numrows();
        //$config['per_page'] = $limit;
        //$CI->pagination->initialize($config);
        //$this->add('paging', $CI->pagination->create_links());
        
        $this->add('subjectTX', $this->sort_column('Subject', 'subject', $sort_type, $base_url.'/'.$offset, $sort_field));
        $this->add('keyTX', $this->sort_column('Key', 'key', $sort_type, $base_url.'/'.$offset, $sort_field));
        $this->add('bodyTX', $this->sort_column('Body', 'body', $sort_type, $base_url.'/'.$offset, $sort_field));
    }
}